package com.example.ten.Utils;

import android.content.Context;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class SaveDataUtils {

    /**
     * @des
     *      写入数据到内部存储当中
     * @param
     *      fileName :文件的名称
     * @param
     *      msg  :要求写入的内容
     * */
    public static void writeDataToInternal(Context context,String fileName, String msg){

        try {
            FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            byte[] data = msg.getBytes();
            fos.write(data);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @des
     *      从内部存储当中读取数据的方法
     * @param
     *      fileName 文件的名称
     * */
    public static String readDataFromInternal(Context context,String fileName){
        String msg = "";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            FileInputStream fis = context.openFileInput(fileName);
            int hasRead = 0;
            byte[]buf = new byte[1024];
            while (true){
                hasRead = fis.read(buf);
                if (hasRead == -1) {
                    break;
                }
                baos.write(buf,0,hasRead);
            }
            msg = baos.toString();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }
}